<template>
  <el-row :gutter="40" class="panel-group">
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
        <div class="card-panel-icon-wrapper icon-people">
          <svg-icon icon-class="peoples" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            土壤水分
          </div>
           <span class="card-panel-num">{{soilMoisture}}%</span>          
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('messages')">
        <div class="card-panel-icon-wrapper icon-message">
          <svg-icon icon-class="message" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            温度监测
          </div>
          <span class="card-panel-num">{{temperature}}°C</span>   
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('purchases')">
        <div class="card-panel-icon-wrapper icon-money">
          <svg-icon icon-class="money" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            土壤pH值
          </div>
          <span class="card-panel-num">{{soilPH}}</span>   
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('shoppings')">
        <div class="card-panel-icon-wrapper icon-shopping">
          <svg-icon icon-class="shopping" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            土壤电导率
          </div>
          <span class="card-panel-num">{{soilConductivity}}mS/cm</span>   
        </div>
      </div>
    </el-col>
    <el-dialog :visible.sync="alertVisible" :title="alertLevel === 'high' ? '严重告警' : '一般告警'" width="30%">
      <div :class="['alert-content', alertLevel]">
        <i :class="alertLevel === 'high' ? 'el-icon-warning' : 'el-icon-info'"></i>
        <span>{{ alertMessage }}</span>
      </div>
    </el-dialog>
  </el-row>
  
</template>

<script>
// import CountTo from 'vue-count-to'
// import Countup from 'vue-countupjs'
import { getData, subscribe } from "@/api/system/realtime";
export default {
  data(){
    return {
      soilMoisture: 30.5678,
      decimalPlaces: 3, // 显示3位小数
      temperature: 21,
      soilPH: 12,
      soilConductivity: 1198,
      timer: null,
      interval: 5000, // 5秒
      
      // 查询参数
      queryParams: {
          pageNum: 1,
          pageSize: 10,
          deviceSn: undefined
        },
      alertVisible: false,
      alertMessage: '',
      alertLevel: 'normal', // 'normal' or 'high'
    }
  },
  components: {
    // Countup 
  },
  mounted() {
    // 组件挂载后立即执行一次
    this.getRealTimeData()
    // 然后定时执行
    this.timer = setInterval(this.getRealTimeData, this.interval)
  },
    
  created() {
      this.subscribeTopic();
      this.getRealTimeData();
    },
  methods: {
    triggerAlert(message, level) {
      this.alertLevel = level;
      this.alertMessage = message;
      this.alertVisible = true;
      
      // 高等级告警持续更久
      const duration = level === 'high' ? 5000 : 3000;
      setTimeout(() => {
        this.alertVisible = false;
      }, duration);
      
    },
    onReady(instance) {
      instance.start() // 必须调用 start() 方法
    },
    subscribeTopic(){
      this.loading = true;
      subscribe().then(response => {
        console.log(response);
      })  
    },
    /** 查询实时监测数据 */
    getRealTimeData() {
        this.loading = true;
        getData(this.addDateRange(this.queryParams)).then(response => {
          this.soilMoisture = response.data.soilMoisture;
          this.temperature = response.data.temperatureActual;
          this.soilPH = response.data.soilPH;
          this.soilConductivity = response.data.soilConductivity;

            console.log(response.data.alarmList);

            if (response.data.alarmList.length > 0){
              var deviceSn = response.data.alarmList[0].deviceSn
              var thresholdValue = response.data.alarmList[0].thresholdValue
              this.triggerAlert(`严重告警: 设备数值 ${deviceSn} 超过临界值 ${thresholdValue}`, 'high');
            }
            
            this.loading = false;
          }
        );
      },
    handleSetLineChartData(type) {
      this.$emit('handleSetLineChartData', type)
    },
    myMethod() {
      console.log('生命周期定时执行', new Date())
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

<style lang="scss" scoped>
.panel-group {
  margin-top: 108px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-money {
        background: #f4516c;
      }

      .icon-shopping {
        background: #34bfa3
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
</style>
